package client;

import java.io.*;
import java.net.Socket;

import server.Server;
import unit.Player;

public class LoginHandeler implements Runnable {
	private Socket socket;
	private ClientControl client;
	
	public LoginHandeler(ClientControl client, Socket sock){
		this.socket = sock;
		this.client = client;
		new Thread(this).start();
	}

	@Override
	public void run() {
		BufferedWriter out;
		BufferedReader in;
		try {
			out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		} catch (IOException e1) {return;}
		
		while (true) {
			try {
				String inline = in.readLine();
				if (inline.startsWith("login")) {
					String[] parts = inline.split(" ");
					if (parts[2].equals("pass")) {
						//server.addClient(new ClientControl(socket, server));
						//Player p = server.addPlayer("jees", socket);
						Player p = client.loggedIn(parts[1]);
						out.write("hello "+ p.id() +" "+ 0 +" "+ p.getPos().getX() +" "+ p.getPos().getY() +"\n");
						out.flush();
						return;
					} else {
						out.write("no go\n");
						out.flush();
					}
				} else {
					out.write("not logged in\n");
					out.flush();
				}
			} catch (IOException e) {}
		}
	}

}
